package com.summer.shopping.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.summer.shopping.domain.common.req.ReqHolder;
import com.summer.shopping.domain.table.Account;
import com.summer.shopping.domain.table.Orders;
import com.summer.shopping.domain.table.dto.OrderCreateDTO;
import com.summer.shopping.domain.table.dto.PageDTO;
import com.summer.shopping.domain.table.vo.PayAmountVo;
import com.summer.shopping.mapper.AccountMapper;
import com.summer.shopping.service.OrderService;
import com.summer.shopping.utils.Result;
import com.summer.shopping.utils.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin("*")
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private AccountMapper accountMapper;

    //创建订单（去支付）
    @PostMapping("create")
    public Result createOrder(@RequestBody OrderCreateDTO orderCreateDTO){

        if(orderCreateDTO.getNumbers().size()!=orderCreateDTO.getProductIds().size())
            return Result.error("商品与商品数目不匹配");

        return orderService.createOrder(orderCreateDTO);
    }

    //查询需要支付的订单接口
    @GetMapping("pay/query/{orderId}")
    public Result queryPay(@PathVariable Integer orderId){

        return orderService.queryPay(orderId);
    }

    //我的订单
    @GetMapping("my")
    public Result myOrder(PageDTO pageDTO){
        return orderService.myOrder(pageDTO);
    }

    //支付
    @PostMapping("pay/{id}")
    public Result  pay(@PathVariable Integer id){

        return orderService.pay(id);
    }
    @PutMapping("cancel/{id}")
    public Result cancel(@PathVariable Integer id){
        Orders orders = orderService.getById(id);
        if(orders ==null){
            return Result.error("订单取消失败");
        }
        orders.setOrderStatus(Constants.CANCELLED);
        orderService.updateById(orders);
        return Result.ok("订单已取消");
    }
    @PostMapping("open")
    public Result open(){
        Integer balance = 1000000;//一百万
        Account account = new Account();
        account.setBalance(Double.valueOf(balance));
        Long userId = ReqHolder.getUserId();
        account.setUserId(userId);
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("user_id",userId);
        Account account1 = accountMapper.selectOne(accountQueryWrapper);
        if(account1!=null)return Result.error("不可重复开户");
        accountMapper.insert(account);
        return Result.ok("开户成功！！！");
    }

    @GetMapping("judgeOpen")
    public Result judge(){
        Long userId = ReqHolder.getUserId();
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("user_id",userId);
        Account account1 = accountMapper.selectOne(accountQueryWrapper);
        if(account1!=null)return Result.error("已开户");
        else return Result.ok("未开户");
    }

    @GetMapping("statistics")
    public Result statistics(){
        Long userId = ReqHolder.getUserId();
        QueryWrapper<Orders> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id",userId);
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("user_id",userId);
        Account account1 = accountMapper.selectOne(accountQueryWrapper);
        //总消费
        Double consumption = account1.getConsumption();
        //订单总数
        Long count = orderService.count(userQueryWrapper);
        PayAmountVo payAmountVo = new PayAmountVo(consumption, count);
        return Result.ok(payAmountVo);
    }

}
